package com.wl4837.remote.deploy.factory;

import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindowFactory;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;

public class MyToolWindowFactory implements ToolWindowFactory {

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 创建 TabbedPane
        JTabbedPane tabbedPane = new JTabbedPane();

        // 添加第一个 Tab：项目信息
        JPanel projectInfoPanel = new JPanel(new BorderLayout());
        projectInfoPanel.setBorder(JBUI.Borders.empty(10));
        projectInfoPanel.add(new JLabel("项目名称: " + project.getName()), BorderLayout.NORTH);
        projectInfoPanel.add(new JLabel("项目根目录: " + project.getBasePath()), BorderLayout.SOUTH);
        tabbedPane.addTab("项目信息", projectInfoPanel);

        // 添加第二个 Tab：任务列表
        JPanel taskPanel = new JPanel(new BorderLayout());
        taskPanel.setBorder(JBUI.Borders.empty(10));
        String[] columnNames = {"ID", "名称", "状态"};
        Object[][] data = {
                {1, "任务1", "进行中"},
                {2, "任务2", "已完成"},
                {3, "任务3", "待开始"}
        };
        JTable table = new JTable(new DefaultTableModel(data, columnNames));
        JScrollPane scrollPane = new JScrollPane(table);
        taskPanel.add(scrollPane, BorderLayout.CENTER);
        tabbedPane.addTab("任务列表", taskPanel);

        // 添加第三个 Tab：操作按钮
        JPanel buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.setBorder(JBUI.Borders.empty(10));
        JButton button = new JButton("Click Me");
        button.addActionListener(e -> JOptionPane.showMessageDialog(buttonPanel, "Button clicked!"));
        buttonPanel.add(button, BorderLayout.CENTER);
        tabbedPane.addTab("操作", buttonPanel);

        // 添加第四个 Tab：树结构
        JPanel treePanel = new JPanel(new BorderLayout());
        treePanel.setBorder(JBUI.Borders.empty(10)); // 增加内边距

        // 创建树结构
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("根节点");
        DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("节点1");
        DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("节点2");
        DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("节点3");
        root.add(node1);
        root.add(node2);
        node2.add(node3);
        JTree tree = new JTree(root);
        JScrollPane treeScrollPane = new JScrollPane(tree);
        treePanel.add(treeScrollPane, BorderLayout.CENTER);
        tabbedPane.addTab("树结构", treePanel);

        // 将 TabbedPane 添加到主面板
        mainPanel.add(tabbedPane, BorderLayout.CENTER);

        // 创建 Content 并添加到 ToolWindow
        Content content = ContentFactory.SERVICE.getInstance().createContent(mainPanel, "", false);
        toolWindow.getContentManager().addContent(content);
    }

}
